<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('终端划拨')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-record-add" enctype="multipart/form-data">
            <h4 class="form-header h4" >划拨目标信息</h4>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label  is-required">目标服务商编号：</label>
                        <div class="col-sm-8">
                            <input name="agentTarNum" onclick="selectAgent()" readonly id="xagentNum" type="text" class="form-control" required>
                            <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label is-required">目标服务商名称：</label>
                        <div class="col-sm-8">
                            <input name="agentTarName" class="form-control" id="xagentName" type="text" required readonly>
                        </div>
                    </div>
                </div>
            </div>

            <!--<div id="nextAgentShow" style="display: none">
                <div class="form-group">
                    <label class="col-sm-3 control-label  is-required">目标下级编号：</label>
                    <div class="col-sm-8">
                        <input name="nextAgentNum" onclick="selectNextAgentInfo()" readonly id="nextAgentNum" type="text" class="form-control" required>
                        <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">目标下级名称：</label>
                    <div class="col-sm-8">
                        <input name="nextAgentName" class="form-control" id="nextAgentName" type="text" required readonly>
                    </div>
                </div>
            </div>-->


            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">政策：</label>
                        <div class="col-sm-8">
                            <input name="actName" onclick="selectAct()" readonly id="actName" type="text" class="form-control">
                            <input name="actId" id="actId" type="hidden">
                            <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">截止时间：</label>
                        <div class="col-sm-8">
                            <input name="endDate" required readonly class="form-control time-input" placeholder="yyyy-MM-dd" type="text">
                        </div>
                    </div>
                </div>
            </div>
            <!--<div class="row">
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-3 control-label is-required">激活返现：</label>
                            <div class="col-sm-8">
                                <select name="activeType"  class="form-control" >
                                    <option value="1">反下级</option>
                                    <option value="0">反本级</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <div class="form-group">
                            <label class="col-sm-3 control-label is-required">达标返现：</label>
                            <div class="col-sm-8">
                                <select name="standardType"  class="form-control" >
                                    <option value="1">反下级</option>
                                    <option value="0">反本级</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label is-required">阶段达标返现：</label>
                        <div class="col-sm-8">
                            <select name="stageStandardType"  class="form-control" >
                                <option value="1">反下级</option>
                                <option value="0">反本级</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label is-required">流量费返现：</label>
                        <div class="col-sm-8">
                            <select name="flowType"  class="form-control" >
                                <option value="1">反下级</option>
                                <option value="0">反本级</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label is-required">服务费返现：</label>
                        <div class="col-sm-8">
                            <select name="serviceType"  class="form-control" >
                                <option value="1">反下级</option>
                                <option value="0">反本级</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>-->
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">流量费：</label>
                        <div class="col-sm-8">
                            <select name="sim_fee" class="form-control m-b" th:with="type=${@dict.getType('sim_fee')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                            <span id="sim" style="color: blue" class="help-block m-b-none"><i class="fa fa-info-circle"></i>返现金额 0 元</span>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">收取天数：</label>
                        <div class="col-sm-8">
                            <select name="sim_end" class="form-control m-b" th:with="type=${@dict.getType('take_day')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>
                </div>

            </div>
            <div class="row">

                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">服务费：</label>
                        <div class="col-sm-8">
                            <select name="service_fee" class="form-control m-b" th:with="type=${@dict.getType('service_fee')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                            <span style="color: blue" id="service" class="help-block m-b-none"><i class="fa fa-info-circle"></i>返现金额 0 元</span>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">收取天数：</label>
                        <div class="col-sm-8">
                            <select name="service_end" class="form-control m-b" th:with="type=${@dict.getType('take_day')}">
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">划拨类型：</label>
                        <div class="col-sm-6" style="margin-top: 5px;">
                            <label style="text-align: center"> <input type="radio" onchange="javascript:window.onchangeRadio(this)" name="hbType" value="1"/> SN编号 </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            <label style="text-align: center"> <input type="radio" onchange="javascript:window.onchangeRadio(this)" name="hbType" value="2"/> 文件导入</label>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="form-group"  >
                        <label class="col-sm-3 control-label is-required">划拨台数：</label>
                        <div class="col-sm-8">
                            <input name="total" id="total" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')" class="form-control" type="text" required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group" id="excel" style="display: none">
                        <label class="col-sm-3 control-label is-required">终端文件：</label>
                        <div class="col-sm-8">
                            <span class="btn btn-white btn-file">
                                <input id="files" type="file" name="file"  accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
                            </span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-6">
                    <div class="form-group" id="sn" style="display: none">
                        <div class="form-inline">
                            <label class="col-sm-3 control-label is-required">SN编号：</label>
                            <div class="col-sm-8">
                                <input type="text"  class="form-control" id="startSn" name="startSn">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
                <div class="form-group" style="display: none">
                <label class="col-sm-3 control-label is-required">档位设置：</label>
                <div class="col-sm-8">
                    <select name="isSetGrade" readonly  class="form-control" onchange="selectOnchangeDw(this)">
                        <option value="-1">不分配</option>
                        <option value="1">分配</option>
                    </select>
                </div>
               </div>
                <div class="form-group" style="display: none" id="fpdw" >
                <label class="col-sm-3 control-label">分配档位：</label>
                <div class="col-sm-6" style="margin-top: 5px;">
                    <label > <input type="radio"  name="selectNormNum" value="V3"/> V3 </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label > <input type="radio"  name="selectNormNum" value="V5"/> V5 </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label > <input type="radio"  name="selectNormNum" value="V7"/> V7 </label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <label > <input type="radio"  name="selectNormNum" value="V9"/> V9 </label>
                </div>
            </div>
            <div class="form-group" style="display: none" id="fcqx">
                <label class="col-sm-3 control-label is-required">扶持期限：</label>
                <div class="col-sm-8">
                    <select name="selectDate" readonly  class="form-control" >
                        <option value="3">1个月</option>
                        <option value="6">6个月</option>
                        <option value="9">9个月</option>
                        <option value="12">1年</option>
                    </select>
                </div>
            </div>
            <div style="display: none" id="sq">
            <h4 class="form-header h4" >申请服务商信息</h4>
            <div class="row">
                <div class="form-group">
                    <label class="col-sm-3 control-label  is-required">服务商编号：</label>
                    <div class="col-sm-8">
                        <input name="applyAgentNum" onclick="selectAgentInfoHb()" readonly id="sqXagentNum" type="text" class="form-control" required>
                        <i class="fa fa-search" style="margin-top: -22px;margin-right:3px;float:right;"></i>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">服务商名称：</label>
                    <div class="col-sm-8">
                        <input name="applyAgentName" class="form-control" id="sqXagentName" type="text" required readonly>
                    </div>
                </div>
            </div>
            </div>

        </form>
        <div class="row">
            <div class="form-group" style="text-align: center">
                <div class="col-sm-8" style="color: red; text-align: center" hidden id="tishi">温馨提示：请确认选择活动内容是否正确，如无问题请再次点击“确认”进行划拨</div>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "terminal/record"
        var num = 0;
        $("#form-record-add").validate({
            focusCleanup: true

        });

        /**
         * 上传文件成功后触发的事件
         * result data 读取文件划拨台数
         */
        $("#files").change(function (e){
            var fileData = new FormData();
            if ($("[name = 'file']")[0].files[0]==undefined){
                $("#total").val('');
                return;
            }else{
                fileData.append("file", $("[name = 'file']")[0].files[0]);
                $.ajax({
                    url : prefix + "/file",
                    type: 'POST',
                    data: fileData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success : function(result) {
                        $("#total").val(result.data);
                    },
                    error : function(result) {
                        $.modal.alertError(result.msg);
                    }
                })
            }
        })

        function selectChange(obj) {
            if (obj.value == '1'){
                $("#nextAgentShow").show();
            } else {
                $("#nextAgentShow").hide();

            }

        }
        function submitHandler() {
            if(num == 0){
                num ++;
                $("#tishi").show();
                return;
            }
            if ($.validate.form()) {
                var formData = new FormData();
                formData.append("agentTarNum",$("[name='agentTarNum']").val());     //目标代理商编号
                formData.append("agentTarName",$("[name='agentTarName']").val());   //目标代理商名称
                formData.append("total",$("[name='total']").val());                 //划拨数量
                formData.append("type",$("[name='posType']").val());                //终端类型
                formData.append("actId",$("[name='actId']").val());                 //活动
                formData.append("endDate",$("[name='endDate']").val());             //到期时间
                formData.append("isSetGrade",$("[name='isSetGrade']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("applyAgentNum",$("[name='applyAgentNum']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("applyAgentName",$("[name='applyAgentName']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("sim_fee",$("[name='sim_fee']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("sim_end",$("[name='sim_end']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("service_fee",$("[name='service_fee']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("service_end",$("[name='service_end']").val());       //是否分配档位 -1 不分配 1 分配
               /* formData.append("activeType",$("[name='activeType']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("standardType",$("[name='standardType']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("stageStandardType",$("[name='stageStandardType']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("flowType",$("[name='flowType']").val());       //是否分配档位 -1 不分配 1 分配
                formData.append("serviceType",$("[name='serviceType']").val());       //是否分配档位 -1 不分配 1 分配*/

                //划拨类型选择(必选一个)
                var hbType = $('input[name="hbType"]:checked ').val();
                var type = '';
                if (typeof (hbType) == "undefined") {
                    $.modal.alertError("请选择划拨类型");
                    return;
                }
                //如果是文件
                if (hbType == '2'){
                    formData.append("file", $("[name = 'file']")[0].files[0]);
                    type = '2';
                }
                //sn
                if (hbType == '1') {
                   var startSn = $("#startSn").val()
                    if (startSn.length == 0) {
                        $.modal.alertError("请选择SN起始编号");
                        return;
                    }
                    formData.append('startSn', startSn);
                    type = '1';
                }
                formData.append('types', type);
                //选择设置档位才可分配档位
                if ($("[name='isSetGrade']").val() == '1'){
                    var selectNormNum = $("input[name='selectNormNum']:checked").val();
                    if ($.common.isNotEmpty(selectNormNum)){
                        formData.append("selectNormNum",selectNormNum);
                    }
                    formData.append("selectDate",$("[name='selectDate']").val())
                }




                $.ajax({
                    url : prefix + "/add",
                    type: 'POST',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    beforeSend: function () {
                        $.modal.loading("正在处理中，请稍后...");
                        $.modal.disable();
                    },
                    success : function(result) {
                        if (result.code == web_status.SUCCESS) {
                            var parent = window.parent;
                            if (parent.table.options.type == table_type.bootstrapTable) {
                                $.modal.close();
                                parent.$.modal.msgSuccess(result.msg);
                                parent.$.table.refresh();
                            } else if (parent.table.options.type == table_type.bootstrapTreeTable) {
                                $.modal.close();
                                parent.$.modal.msgSuccess(result.msg);
                                parent.$.treeTable.refresh();
                            } else {
                                $.modal.msgReload("保存成功,正在刷新数据请稍后……", modal_status.SUCCESS);
                            }
                        } else if (result.code == web_status.WARNING) {
                            $.modal.alertWarning(result.msg)
                        }  else {
                            $.modal.alertError(result.msg);
                        }
                        $.modal.closeLoading();
                        $.modal.enable();
                        //$.modal.alertSuccess(data.msg)
                    },
                    error : function(result) {
                        $.modal.alertError(result.msg);
                    }
                });
            }
        }
        function selectAgent() {
            var url =prefix+ "/selectAgentInfo";
            $.modal.open("选择服务商" , url, '1000',"600");
        }
        function selectNextAgentInfo() {
            var topAgentNum = $("#xagentNum").val();
            if ($.common.isEmpty($("#xagentNum").val())){
                $.modal.alertWarning("请选择目标服务商")
                return;
            }
            var url =prefix+ "/selectNextAgentInfo?topAgentNum="+topAgentNum;
            $.modal.open("选择服务商" , url, '1000',"600");
        }
        function selectAgentInfoHb() {
            var url =prefix+ "/selectAgentInfoHb";
            $.modal.open("选择服务商" , url, '1000',"600");
        }

        function selectAct() {
            var url =prefix+ "/selectActList";
            $.modal.open("选择政策" , url, '700');
        }

        /**
         * radio的监听操作
         * */
        function onchangeRadio(obj) {
            if (obj.checked) {
                //sn
                if (obj.value == '1') {
                    $("#sn").show();
                    $("#excel").hide();
                    $("#total").attr('readonly',false);
                }
                //文件
                if (obj.value == '2') {
                    $("#excel").show();
                    $("#sn").hide();
                    $("#total").attr('readonly',true);
                }
            }
        }

        /**
         * 初始化radio标签全部未选
         */
        function radioInit() {
            var val = $('input:radio[name="hbType"]:checked').val();
            if (val != "undefined") {
                $('input:radio[name="hbType"]').removeAttr('checked');
            }
        };

        /**
         * 监听是否分配档位选择的值
         */
        function selectOnchangeDw(obj) {
            //不设置
            if (obj.value == '1'){
                $("#fpdw").show();
                $("#fcqx").show();
                $("#sq").show();

            }
            //不设置
            if (obj.value == '-1'){
                $("#fpdw").hide();
                $("#fcqx").hide();
                $("#sq").hide();

            }

        }


    </script>
</body>
</html>
